package com.example.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.exception.CustomException;
import com.example.common.utils.Constant;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;
import com.example.modules.sys.dao.TemperatureDao;
import com.example.modules.sys.entity.TemperatureEntity;
import com.example.modules.sys.service.TemperatureService;
import com.example.modules.sys.shiro.ShiroUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;


@Service("temperatureService")
public class TemperatureServiceImpl extends ServiceImpl<TemperatureDao, TemperatureEntity> implements TemperatureService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TemperatureEntity> page = this.page(
                new Query<TemperatureEntity>().getPage(params),
                new QueryWrapper<TemperatureEntity>()
                        // 数据权限
                        .apply(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );

        // page.

        return new PageUtils(page);
    }

    @Override
    public boolean save(TemperatureEntity entity) {
        // 获取当前学生
        Long userId = ShiroUtils.getUserId();
        entity.setUserId(userId);

        LocalDateTime now = LocalDateTime.now();
        final int nowHour = now.getHour();
        // 如果当前不在填表时间内
        if (nowHour < 6 || nowHour >= 21) {
            throw new CustomException("当前时间段不可填写体温");
        }

        return super.save(entity);
    }
}
